package com.unionPay.data;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxHandler extends DefaultHandler {

	private StringBuffer buffer = new StringBuffer();

	private final String RESPCODE_NODE = "respCode";
	private final String RESPDESC_NODE = "respDesc";
	private boolean inrespCode = false;
	private boolean inrespDesc = false;

	private final String merchantId_node = "merchantId";
	private final String merchantOrderId_node = "merchantOrderId";
	private final String merchantOrderTime_node = "merchantOrderTime";
	private final String merchantOrderAmt_node = "merchantOrderAmt";
	private final String merchantUserId_node = "merchantUserId";
	private final String gwInvokeCmd_node = "gwInvokeCmd";
	private final String mobileNum_node = "mobileNum";
	private final String msgExt_node = "msgExt";
	private final String card_number = "cardNum";

	private boolean inmerchantId = false;
	private boolean inmerchantOrderId = false;
	private boolean inmerchantOrderTime = false;
	private boolean inmerchantOrderAmt = false;
	private boolean inmerchantUserId = false;
	private boolean ingwInvokeCmd = false;
	private boolean inmobileNum = false;
	private boolean inmsgExt = false;
	private boolean incardNum = false;
	private BindPay binPay;

	public BindPay getBinPay() {
		return binPay;
	}

	public void startDocument() throws SAXException {
		super.startDocument();
		binPay = new BindPay();
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		String tag = localName != null ? localName : qName;
		if (tag.equals(merchantId_node)) {
			inmerchantId = true;
		} else if (tag.equals(merchantOrderId_node)) {
			inmerchantOrderId = true;
		} else if (tag.equals(merchantOrderTime_node)) {
			inmerchantOrderTime = true;
		} else if (tag.equals(merchantOrderAmt_node)) {
			inmerchantOrderAmt = true;
		} else if (tag.equals(merchantUserId_node)) {
			inmerchantUserId = true;
		} else if (tag.equals(gwInvokeCmd_node)) {
			ingwInvokeCmd = true;
		} else if (tag.equals(mobileNum_node)) {
			inmobileNum = true;
		} else if (tag.equals(RESPCODE_NODE)) {
			inrespCode = true;
		} else if (tag.equals(RESPDESC_NODE)) {
			inrespDesc = true;
		} else if (tag.equals(msgExt_node)) {
			inmsgExt = true;
		} else if (tag.equals(card_number)) {
			incardNum = true;
		}

	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		buffer.append(ch, start, length);
		String merchantcontent = new String(ch, start, length);

		if (inmerchantId) {
			binPay.setMerchantId(merchantcontent);
		} else if (inmerchantOrderId) {
			binPay.setMerchantOrderId(merchantcontent);
		} else if (inmerchantOrderTime) {
			binPay.setMerchantOrderTime(merchantcontent);
		} else if (inmerchantOrderAmt) {
			binPay.setMerchantOrderAmt(merchantcontent);
		} else if (inmerchantUserId) {
			binPay.setMerchantUserId(merchantcontent);
		} else if (ingwInvokeCmd) {
			binPay.setGwInvokeCmd(merchantcontent);
		} else if (inmobileNum) {
			binPay.setMobileNum(merchantcontent);
		} else if (incardNum) {
			binPay.setCardNum(merchantcontent);
		} else if (inmsgExt) {
			binPay.setMsgExt(buffer.toString());
		} else if (inrespCode) {
			binPay.setRespCode(merchantcontent);
		} else if (inrespDesc) {
			binPay.setRespDesc(merchantcontent);
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		String tag = localName != null ? localName : qName;
		if (tag.equals(merchantId_node)) {
			inmerchantId = false;
		} else if (tag.equals(merchantOrderId_node)) {
			inmerchantOrderId = false;
		} else if (tag.equals(merchantOrderTime_node)) {
			inmerchantOrderTime = false;
		} else if (tag.equals(merchantOrderAmt_node)) {
			inmerchantOrderAmt = false;
		} else if (tag.equals(merchantUserId_node)) {
			inmerchantUserId = false;
		} else if (tag.equals(gwInvokeCmd_node)) {
			ingwInvokeCmd = false;
		} else if (tag.equals(mobileNum_node)) {
			inmobileNum = false;
		} else if (tag.equals(RESPCODE_NODE)) {
			inrespCode = false;
		} else if (tag.equals(RESPDESC_NODE)) {
			inrespDesc = false;
		} else if (tag.equals(msgExt_node)) {
			inmsgExt = false;
		} else if (tag.equals(card_number)) {
			incardNum = false;
		}
	}

}
